import {
  getPackage_items,
  getPayList,
  getCardInfo, 
  sendPackagrHandle,
  sendCardHandle
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [],
    selectItemsArr: [], //已选择的检测项目
    packageName: '', //套餐名
    packagePrice: '', //套餐价格
    packageEndTime: '', //套餐有效期
    packageRemark: '', //套餐说明
    id: "", //套餐id 
    dataindex: 0, //当前选中选项卡  0套餐 1充值卡
    isShowReckoning: false,
    cardPrice: "", //充值金额
    cardGivePrice: "", //赠送金额
    cardRemark: "", //赠送金额
    cardEndTime: "", //充值卡有效期
    cardId: "",//会员卡id
    read:false,
  },
  handleZanStepperChange(e) {
    const componentId = e.target.dataset.componentId;
    const stepper = e.detail;
    this.setData({
      [`selectItemsArr[${componentId}].count`]: stepper,
    });
  },
  //跳转至添加项目
  goAddProItem: function () {
    wx.navigateTo({
      url: '../addproitem/addproitem?items=' + JSON.stringify(this.data.items),
    })
  },
  //切换选项卡
  toShowhidden: function (e) {
    if (e.currentTarget.dataset.index == this.data.dataindex) {

    } else {
      this.setData({
        dataindex: e.currentTarget.dataset.index
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
 
    let self = this;
    self.setData({
      dataindex: options.tagindex ? options.tagindex : 0, 
      read: options.read?options.read:false
    })
    if (this.data.dataindex == 0) {
      //套餐
      if (options.item) {
        wx.setNavigationBarTitle({
          title: options.read?"查看套餐": "发送套餐"
        })
        let itemInfo = JSON.parse(options.item)
        
        self.setData({
          // items: r.data,
          selectItemsArr: itemInfo.children,
          packageName: itemInfo.title, //套餐名
          packagePrice: itemInfo.price, //套餐价格
          packageEndTime: itemInfo.end, //套餐有效期
          packageRemark: itemInfo.description, //套餐说明
          id: itemInfo.id,
          qc: options.qc,
          has_qcode:options.has_qcode
        })

      } else {
        self.setData({
          // items: r.data
        })
      }
    } else if (this.data.dataindex == 1) {
      //充值卡

      //如果是点击项目进来。自动填上信息展示项目
      if (options.item) {
        wx.setNavigationBarTitle({
          title: "发送充值卡"
        })
        let itemInfo = JSON.parse(options.item)

        self.setData({
          cardPrice: itemInfo.charge, //充值金额,
          cardGivePrice: itemInfo.give, //赠送金额
          cardRemark: itemInfo.description, //充值卡说明
          cardEndTime: itemInfo.end, //充值卡有效期
          cardId: itemInfo.id, 
          qc: options.qc,
          has_qcode: options.has_qcode
        })

      }
    }


  },
   onShow: function () {
  //   let self = this;
  //   if (self.data.dataindex == 0) {
  //     if (app.globalData.packageItem) {
  //       self.data.selectItemsArr = [];
  //       app.globalData.packageItem.map((o, i) => {
  //         o.children.map((oo, ii) => {
  //           if (oo.isSelect) {
  //             oo.count = 1;
  //             self.data.selectItemsArr.push(oo)
  //           }
  //         })
  //       })
  //       self.setData({
  //         items: app.globalData.packageItem,
  //         selectItemsArr: this.data.selectItemsArr
  //       })
  //     }
  //   }

  },
  //点击结账标记
  onReckoning: function () {
    getPayList((r)=>{
      if(r.code==0){
        this.setData({
          payList: r.data.payments,
          isShowReckoning: true
        })
       
      }
    })
   
  },
  //选择支付方式
  pickerPayMethod: function (e) {
    let paymethod = e.currentTarget.dataset.paymethod;

    this.setData({
      currentPayMethod: paymethod
    })
  },
  //关闭结算窗口
  onReckoningClose: function () {
    this.setData({
      isShowReckoning: false
    })
  },
 
  //保存套餐
  onSave: function () { 
    let {qc,id}=this.data;
    sendPackagrHandle(qc, id,"",(r)=>{
      if (r.code == 0) {
        wx.showToast({
          title: "发送成功",
        })
      } else {
        wx.showToast({
          icon: "none",
          title: r.message,
        })
      } 
    })
  },
  //保存充值卡
  onSaveCard: function () { 
    let { qc, cardId } = this.data;
    sendCardHandle(qc, cardId,"",(r)=>{
      if(r.code==0){
        wx.showToast({ 
          title: "发送成功",
        })
      }else{
        wx.showToast({
          icon:"none",
          title: r.message,
        })
      } 
    })
  },
  //支付方式提交
  payMethodConfrim: function () {
    let self = this; 

   

    if (this.data.dataindex == 0) {
      let { qc, id, currentPayMethod } = this.data;
      sendPackagrHandle(qc, id, currentPayMethod.id, (r) => {
        if (r.code == 0) {
          wx.showToast({
            title: "办理成功",
          })
          wx.navigateBack({
            delta:2
          })
        } else {
          wx.showToast({
            icon: "none",
            title: r.message,
          })
        }
      })
      
    } else if (this.data.dataindex == 1) {
      let { qc, cardId, currentPayMethod } = this.data;
      sendCardHandle(qc, cardId, currentPayMethod.id, (r) => {
        if (r.code == 0) {
          wx.showToast({
            title: "办理成功",
          })
          wx.navigateBack({
            delta: 2
          })
        } else {
          wx.showToast({
            icon: "none",
            title: r.message,
          })
        }
      })
    }

  },
 
})